from flask import Blueprint, request, jsonify, current_app
from flask_jwt_extended import create_access_token, create_refresh_token, jwt_required, get_jwt_identity, get_jwt
import sys
sys.path.append("/")
from app.services.user import UserService
from app.services.financial import FinancialService
from app.schemas.financial import financial_schema, financiales_schema

financial_bp = Blueprint('financial', __name__, url_prefix='/financial')

#本部门成员查看本部门某一条业务信息
@financial_bp.route('/department_user_get_business/<int:business_id>', methods=['GET'])
@jwt_required()
def department_user_get_one_business(business_id):

    current_user_id = get_jwt_identity()

    user = UserService.get_user_by_id(current_user_id)
    if not user:
        return jsonify({"error": "User not found"}), 404


    is_department_user = FinancialService.check_user_department(current_user_id)
    if not is_department_user:
        return jsonify({"error": "User is not department user"}), 404


    business = FinancialService.get_business_by_id(business_id)


    return jsonify(financial_schema.dump(business)), 200
#发出的access token Headers  Authorization  Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTc1MzMzNzU2NywianRpIjoiNzdjNDQ0ZjgtZTdjOC00ODAzLWI0NGQtMWU3OTRiMTMxY2NkIiwidHlwZSI6ImFjY2VzcyIsInN1YiI6IjMiLCJuYmYiOjE3NTMzMzc1NjcsImNzcmYiOiI5YmNkNGQ2Yy0xNDkxLTRjYzItOTgyMC04ZjU2N2YxNmJkYjMiLCJleHAiOjE3NTMzNDExNjcsInVzZXJuYW1lIjoic3VzYW4iLCJpc19hZG1pbiI6ZmFsc2UsInJvbGVzIjpbIm5vcm1hbF91c2VyIl19.V1sA_1d-Q0TaFl-RE1-D2AesPSrfLb568G_M_NjeOis

#返回的json
#{
  #"content": "此次支出业务花了200000元",
  #"created_at": "2025-07-29T03:51:08",
  #"dead_line": "2025-08-28T03:51:08",
  #"id": 1,
  #"name": "与a公司一项支出业务"
#}





#本部门成员查看本部门全部业务信息
@financial_bp.route('/department_user_get_business', methods=['GET'])
@jwt_required()
def department_user_get_businesses():

    current_user_id = get_jwt_identity()

    user = UserService.get_user_by_id(current_user_id)
    if not user:
        return jsonify({"error": "User not found"}), 404


    is_department_user = FinancialService.check_user_department(current_user_id)
    if not is_department_user:
        return jsonify({"error": "User is not department user"}), 404


    businesses = FinancialService.get_all_businesses()


    return jsonify(financiales_schema.dump(businesses)), 200

#发出的access token Headers  Authorization  Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTc1MzMzNzU2NywianRpIjoiNzdjNDQ0ZjgtZTdjOC00ODAzLWI0NGQtMWU3OTRiMTMxY2NkIiwidHlwZSI6ImFjY2VzcyIsInN1YiI6IjMiLCJuYmYiOjE3NTMzMzc1NjcsImNzcmYiOiI5YmNkNGQ2Yy0xNDkxLTRjYzItOTgyMC04ZjU2N2YxNmJkYjMiLCJleHAiOjE3NTMzNDExNjcsInVzZXJuYW1lIjoic3VzYW4iLCJpc19hZG1pbiI6ZmFsc2UsInJvbGVzIjpbIm5vcm1hbF91c2VyIl19.V1sA_1d-Q0TaFl-RE1-D2AesPSrfLb568G_M_NjeOis

#返回的json
#[
  #{
   # "content": "此次支出业务花了200000元",
   # "created_at": "2025-07-29T03:51:08",
   # "dead_line": "2025-08-28T03:51:08",
   # "id": 1,
   # "name": "与a公司一项支出业务"
  #},
  #{
   # "content": "此次收入业务收了300000元",
  #  "created_at": "2025-07-29T03:51:08",
    #"dead_line": "2025-08-28T03:51:08",
   # "id": 2,
   # "name": "与b公司一项收入业务"
  #}
#]




#本部门经理修改本部门某一条业务
@financial_bp.route('/business_info_update/<int:business_id>', methods=['PUT'])
@jwt_required()
def update_business(business_id):

    current_user_id = get_jwt_identity()

    user = UserService.get_user_by_id(current_user_id)
    if not user:
        return jsonify({"error": "User not found"}), 404

    is_manager = FinancialService.check_manager_department(current_user_id)
    if not is_manager:
        return jsonify({"error": "User is not financial manager"}), 404

    data = request.get_json()

    business, error = FinancialService.update_business(business_id, data)

    if error:
        return jsonify({"error": error}), 400
    return jsonify(business), 200
#发出的access token Headers  Authorization  Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTc1MzMzNzU2NywianRpIjoiNzdjNDQ0ZjgtZTdjOC00ODAzLWI0NGQtMWU3OTRiMTMxY2NkIiwidHlwZSI6ImFjY2VzcyIsInN1YiI6IjMiLCJuYmYiOjE3NTMzMzc1NjcsImNzcmYiOiI5YmNkNGQ2Yy0xNDkxLTRjYzItOTgyMC04ZjU2N2YxNmJkYjMiLCJleHAiOjE3NTMzNDExNjcsInVzZXJuYW1lIjoic3VzYW4iLCJpc19hZG1pbiI6ZmFsc2UsInJvbGVzIjpbIm5vcm1hbF91c2VyIl19.V1sA_1d-Q0TaFl-RE1-D2AesPSrfLb568G_M_NjeOis

#发出的json
#{
  #"dead_line": "2025-08-15T15:30:00"
#}
#或
#{
  #"name": "与a公司一项支出类型业务"
#}

#返回的json
#{
  #"content": "此次支出业务花了200000元",
  #"created_at": "2025-07-29T03:51:08",
  #"dead_line": "2025-08-15T15:30:00",
  #"id": 1,
  #"name": "与a公司一项支出业务"
#}



#本部门经理添加本部门一条业务
@financial_bp.route('/business_info_add', methods=['POST'])
@jwt_required()
def add_business():
    current_user_id = get_jwt_identity()

    user = UserService.get_user_by_id(current_user_id)
    if not user:
        return jsonify({"error": "User not found"}), 404

    is_manager = FinancialService.check_manager_department(current_user_id)
    if not is_manager:
        return jsonify({"error": "User is not financial manager"}), 404

    data = request.get_json()

    business, error = FinancialService.add_business(data)
    if error:
        return jsonify({"error": error}), 400

    return jsonify(business), 200

#发出的access token Headers  Authorization  Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTc1MzMzNzU2NywianRpIjoiNzdjNDQ0ZjgtZTdjOC00ODAzLWI0NGQtMWU3OTRiMTMxY2NkIiwidHlwZSI6ImFjY2VzcyIsInN1YiI6IjMiLCJuYmYiOjE3NTMzMzc1NjcsImNzcmYiOiI5YmNkNGQ2Yy0xNDkxLTRjYzItOTgyMC04ZjU2N2YxNmJkYjMiLCJleHAiOjE3NTMzNDExNjcsInVzZXJuYW1lIjoic3VzYW4iLCJpc19hZG1pbiI6ZmFsc2UsInJvbGVzIjpbIm5vcm1hbF91c2VyIl19.V1sA_1d-Q0TaFl-RE1-D2AesPSrfLb568G_M_NjeOis

#发出的json
#{
 # "content": "此次支出业务花了600000元",
 # "dead_line": "2025-08-15T15:30:00",
 # "name": "与d公司一项支出类型业务"
#}

#返回的json
#{
  #"content": "此次支出业务花了600000元",
  #"created_at": "2025-07-29T07:14:35",
  #"dead_line": "2025-08-15T15:30:00",
  #"id": 4,
  #"name": "与d公司一项支出类型业务"
#}



#本部门经理删除本部门一条业务
@financial_bp.route('/business_info_delete/<int:business_id>', methods=['DELETE'])
@jwt_required()
def delete_business(business_id):
    current_user_id = get_jwt_identity()

    user = UserService.get_user_by_id(current_user_id)
    if not user:
        return jsonify({"error": "User not found"}), 404

    is_manager = FinancialService.check_manager_department(current_user_id)
    if not is_manager:
        return jsonify({"error": "User is not financial manager"}), 404


    success, error = FinancialService.delete_business(business_id)

    if error:
        return jsonify({"error": error}), 404

    return jsonify({"message": "Business deleted"}), 200

#发出的access token Headers  Authorization  Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTc1MzMzNzU2NywianRpIjoiNzdjNDQ0ZjgtZTdjOC00ODAzLWI0NGQtMWU3OTRiMTMxY2NkIiwidHlwZSI6ImFjY2VzcyIsInN1YiI6IjMiLCJuYmYiOjE3NTMzMzc1NjcsImNzcmYiOiI5YmNkNGQ2Yy0xNDkxLTRjYzItOTgyMC04ZjU2N2YxNmJkYjMiLCJleHAiOjE3NTMzNDExNjcsInVzZXJuYW1lIjoic3VzYW4iLCJpc19hZG1pbiI6ZmFsc2UsInJvbGVzIjpbIm5vcm1hbF91c2VyIl19.V1sA_1d-Q0TaFl-RE1-D2AesPSrfLb568G_M_NjeOis

#返回的json
#{
  #"message": "Business deleted"
#}